/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistense;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import model.Operacao;

/**
 *
 * @author Aurelio
 */
public class OperacaoDAO {
     private static OperacaoDAO instance = new OperacaoDAO();
    
    private OperacaoDAO(){
    }
    
    public static OperacaoDAO getInstance(){
        return instance;
    }
    
    public void salvar(Operacao op)throws SQLException, ClassNotFoundException{
        Connection conn = null;
        PreparedStatement pst = null;

        try {
              conn = DataBaseLocator.getInsLocator().getConnection();
              pst = conn.prepareStatement("insert into Operacao (valor, id_conta, data) values(?,?,?)");
              pst.setDouble(1, op.getValor());
              pst.setInt(2, op.getConta().getId());
              pst.setDate(3, new Date(Calendar.getInstance().getTimeInMillis()));
              pst.executeUpdate();
              
        } catch(SQLException e) {
                e.printStackTrace();
        } finally {
                closeResources(conn, pst);
        }
    }
    
    public void closeResources(Connection conn, Statement st){
      try {
            if(st!=null) st.close();
            if(conn!=null) conn.close();
          } catch(SQLException e) {
                    
          }
    }
}
